<template>
  <div class="bottom-bar">
    <div class="item-selector">
      <check-button
        class="check-button"
        :isChecked="isSelectAll"
        @click.native="checkClick"
      />
      <span>全选</span>
    </div>
    <div class="price">合计:{{ totalPrice }}</div>
    <div class="calc" @click="calClick">去结算({{ checkedLength }})</div>
  </div>
</template>

<script>
import CheckButton from "../../../components/content/checkButton/CheckButton.vue";
export default {
  components: { CheckButton },
  computed: {
    totalPrice() {
      return (
        "￥" +
        this.$store.state.cartList
          .filter((item) => {
            return item.checked;
          })
          .reduce((preValue, item) => {
            return preValue + item.price * item.count;
          }, 0)
          .toFixed(2)
      );
    },
    checkedLength() {
      return this.$store.state.cartList.filter((item) => item.checked).length;
    },
    isSelectAll() {
      if (this.$store.state.cartList.length === 0) {
        return false;
      }
      return !this.$store.state.cartList.filter((item) => !item.checked).length;
    },
  },
  methods: {
    checkClick() {
      if (this.isSelectAll) {
        //全都选中
        this.$store.state.cartList.forEach((element) => {
          console.log(element.checked);
          element.checked = false;
        });
      } else {
        this.$store.state.cartList.forEach((element) => {
          element.checked = true;
        });
      }
    },
    calClick() {
      if (!this.$store.state.cartList.every((item) => item.checked)) {
        console.log(this.$toast);
        this.$toast.show("请选择购买的商品", 2000);
      } else {
        console.log("都选中了");
      }
    },
  },
};
</script>

<style scoped>
.bottom-bar {
  display: flex;
  height: 40px;
  line-height: 40px;
  width: 100%;
  background-color: #eee;
  position: relative;
  bottom: 43px;
  left: 0px;
  right: 0;
  font-size: 14px;
}
.item-selector {
  display: flex;
  align-items: center;
  margin-left: 10px;
  width: 60px;
}
.check-button {
  width: 22px;
  height: 22px;
  line-height: 22px;
  margin-right: 5px;
}
.price {
  margin-left: 20px;
  flex: 1;
}
.calc {
  width: 90px;
  background-color: #ff0002;
  color: #fff;
  text-align: center;
}
</style>